package com.emr.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;



/**
 * 全局设置
 * @author Hearken
 * @date Sep 26, 2010 2:56:07 PM
 */
public class GlobalSetting {
	
	public static final String PROJECT_CHARSET = "UTF-8";
	public static final String CONFIG_FILE_NAME = "applicationConfigure.properties";
	public static SafeProperties CONFIG_PROPERTIES = null;
	// 日志与权限配对
	public static HashMap<String, String> LOG_COMPETENCE = null;
	private static MD5 md5 = new MD5();
	public static final String SUPER_ADMIN_PWD = md5.getMD5ofStr("richq-admin-823923");
	// session名
	public static final String SESSION_USER_LOGIN_INFO = "UserLoginInfos";
	public static final String SESSION_VALIDATE_CODE = "ValidateCode";
	
	/**
	 * 发卡信息差异比较文件上传路径
	 * @return
	 */
	public static String getPpopContrastUploadPath() {
		return getProStrValue("ppop.contrast.uploadPath");
	}
	
	/**
	 * 发卡信息差异比较文件下载路径
	 * @return
	 */
	public static String getPpopContrastDownloadPath() {
		return getProStrValue("ppop.contrast.downloadPath");
	}
	
	/**
	 * 发卡信息差异比较模板路径
	 * @return
	 */
	public static String getPpopContrastTemplatePath() {
		return getProStrValue("ppop.contrast.templatePath");
	}
	
	/**
	 * 发卡信息差异比较上传文件大小
	 * @return
	 */
	public static long getPpopContrastFileSize() {
		return getProLonValue("ppop.contrast.fileSize");
	}
	
	/**
	 * 发卡信息导出路径
	 * @return
	 */
	public static String getPpopExportPath() {
		return getProStrValue("ppop.export.path");
	}
	
	/**
	 * 发卡信息导入路径
	 * @return
	 */
	public static String getPpopImportPath() {
		return getProStrValue("ppop.import.path");
	}
	
	/**
	 * 发卡信息导入文件大小
	 * @return
	 */
	public static long getPpopImportFileSize() {
		return getProLonValue("ppop.import.fileSize");
	}
	
	/**
	 * 区域信息同步下载文件路径
	 * @return
	 */
	public static String getRegionSynDataDownloadPath() {
		return getProStrValue("region.synData.downloadPath");
	}
	
	/**
	 * 获取省资料库接口地址
	 * @return
	 */
	public static String getShengzlkUrl() {
		String sUrl = "";
		if ("0".equals(getProjectMode())) {
			sUrl = getProStrValue("webservice.shengzlk.testUrl");
		} else {
			sUrl = getProStrValue("webservice.shengzlk.url");
		}
		return sUrl;
	}
	
	/**
	 * 获取省资料库准入账号
	 * @return
	 */
	public static String getShengzlkAccount() {
		return getProStrValue("webservice.shengzlk.account");
	}
	
	/**
	 * 获取省资料库准入密码
	 * @return
	 */
	public static String getShengzlkPassword() {
		return getProStrValue("webservice.shengzlk.password");
	}
	
	/**
	 * 获取参合人口资料档案下载后保存的路径
	 * @return
	 */
	public static String getSynDataDownloadPath() {
		return getProStrValue("ppop.synData.downloadPath");
	}
	
	/**
	 * 获取参合人口资料档案下载后解压的路径
	 * @return
	 */
	public static String getSynDataOriginalPath() {
		return getProStrValue("ppop.synData.originalPath");
	}
	
	/**
	 * 获取保存参合人口资料档案到数据库成功后文件移到的成功路径
	 * @return
	 */
	public static String getSynDataSuccessPath() {
		return getProStrValue("ppop.synData.successPath");
	}
	
	/**
	 * 获取保存参合人口资料档案到数据库失败后文件移到的失败路径
	 * @return
	 */
	public static String getSynDataFailPath() {
		return getProStrValue("ppop.synData.failPath");
	}
	
	/**
	 * 获取项目的版本 0：测试版，1：正式版
	 * @return
	 */
	public static String getProjectMode() {
		return getProStrValue("project.mode");
	}
	
	/**
	 * 获取监听器下载参合人口档案开始时间
	 * @return
	 */
	public static String getPpopSynDataExeStartTime() {
		return getProStrValue("ppop.synData.exeStartTime");
	}
	
	/**
	 * 获取监听器下载参合人口档案结束时间
	 * @return
	 */
	public static String getPpopSynDataExeEndTime() {
		return getProStrValue("ppop.synData.exeEndTime");
	}
	
	/**
	 * 获取监听器下载参合人口档案的模式（today or yesterday）
	 * @return
	 */
	public static String getPpopSynDataExeMode() {
		return getProStrValue("ppop.synData.exeMode");
	}
	
	/**
	 * 获取数据交换系统调用接口账号
	 * @return
	 */
	public static String getDataExChangeAccount() {
		return getProStrValue("dataExChange.synData.account");
	}
	
	/**
	 * 获取数据交换系统调用接口密码
	 * @return
	 */
	public static String getDataExChangePassword() {
		return getProStrValue("dataExChange.synData.password");
	}
	
	/**
	 * 获取监听器导入发卡信息开始时间
	 * @return
	 */
	public static String getPpopTaskAuditExeStartTime() {
		return getProStrValue("ppop.taskAudit.exeStartTime");
	}
	
	/**
	 * 获取监听器导入发卡信息结束时间
	 * @return
	 */
	public static String getPpopTaskAuditExeEndTime() {
		return getProStrValue("ppop.taskAudit.exeEndTime");
	}
	
	/**
	 * 获取监听器导入发卡信息的模式（today or yesterday）
	 * @return
	 */
	public static String getPpopTaskAuditExeMode() {
		return getProStrValue("ppop.taskAudit.exeMode");
	}
	
	/**
	 * 获取发卡信息上传路径
	 * @return
	 */
	public static String getPpopTaskAuditUploadPath() {
		return getProStrValue("ppop.taskAudit.uploadPath");
	}
	
	/**
	 * 获取发卡信息下载路径
	 * @return
	 */
	public static String getPpopTaskAuditDownloadPath() {
		return getProStrValue("ppop.taskAudit.downloadPath");
	}
	
	/**
	 * 获取后台登录用户名
	 * @return
	 */
	public static String getBackgroundLoginName() {
		return getProStrValue("background.login.name");
	}
	
	/**
	 * 获取后台登录密码
	 * @return
	 */
	public static String getBackgroundLoginPassword() {
		return getProStrValue("background.login.password");
	}
	
	/**
	 * 获取后台登录密码
	 * @return
	 */
	public static String getBackgroundExecPassword() {
		return getProStrValue("background.exec.password");
	}
	
	/**
	 * 获取导出人口资料文件分割行数
	 * @return
	 */
	public static int getPpopExportSegmentation() {
		return GlobalSetting.getProIntValue("ppop.export.segmentation");
	}
	
	/**
	 * 获取区域同步数据下载路径
	 * @return
	 */
	public static String getHospitalinfoDataDownloadPath() {
		return getProStrValue("hospitalinfo.synData.downloadPath");
	}
	
	/**
	 * 获取疾病目录导入文件限制大小
	 * @return
	 */
	public static int getIcdImportFileSize() {
		return getProIntValue("icd.import.fileSize");
	}
	
	/**
	 * 获取疾病目录上传文件保存路径
	 * @return
	 */
	public static String getIcdImportUploadPath() {
		return getProStrValue("icd.import.uploadPath");
	}
	
	/**
	 * 获取疾病目录下载文件保存路径
	 * @return
	 */
	public static String getIcdExportDownloadPath() {
		return getProStrValue("icd.export.downloadPath");
	}
	
	/**
	 * 获取项目路径(例如：d:/w3.2/projectName)
	 * @return
	 */
	public static String getProjectPath() {
		String sProjectPath = "";
		try {
			String sTempPath = GlobalSetting.class.getClassLoader().getResource("").getPath();
			if (sTempPath.length() > 0) {
				sTempPath = sTempPath.substring(1, sTempPath.length());
				if (sTempPath.indexOf("\\") > -1) {
					sTempPath = sTempPath.replace("\\", "/");
				}
			}
			File file = new File(sTempPath);
			if (file.exists()) {
				sProjectPath = file.getAbsolutePath();
				if (sProjectPath.indexOf("\\") > -1) {
					sProjectPath = sProjectPath.replace("\\", "/");
				}
			} else {
				sProjectPath = file.getPath();
				if (sProjectPath.indexOf("\\") > -1) {
					sProjectPath = sProjectPath.replace("\\", "/");
				}
				sProjectPath = "/" + sProjectPath;
			}
			try {
				sProjectPath = URLDecoder.decode(sProjectPath, PROJECT_CHARSET);
			} catch (UnsupportedEncodingException e) {
				System.out.println("获取项目路径时URL解码时程序出错！");
				e.printStackTrace();
			}
			if (sProjectPath.indexOf("/WEB-INF") > -1) {
				sProjectPath = sProjectPath.substring(0, sProjectPath.indexOf("/WEB-INF"));
			}
			if (sProjectPath.indexOf("/buildClass") > -1) {
				sProjectPath = sProjectPath.substring(0, sProjectPath.indexOf("/buildClass")) + "/WebRoot";
			}
			if (sProjectPath.endsWith("/")) {
				sProjectPath = sProjectPath.substring(0, sProjectPath.length() - 1);
			}
			// System.out.println("项目路径=" + sProjectPath);
		} catch (Exception e) {
			System.out.println("获取项目路径时程序出错！");
			e.printStackTrace();
		}
		return sProjectPath;
	}
	
	/**
	 * 获取配置文件的值(String)
	 * @return
	 */
	public static String getProStrValue(String sPropertyName) {
		String sValue = "";
		try {
			if (CONFIG_PROPERTIES == null || CONFIG_PROPERTIES.size() < 1) {
				loadConfigProperty();
				sValue = CONFIG_PROPERTIES.getProperty(sPropertyName, "");
			} else {
				sValue = CONFIG_PROPERTIES.getProperty(sPropertyName, "");
			}
		} catch (Exception e) {
			System.out.println("读取配置文件出错！");
			e.printStackTrace();
		}
		return sValue;
	}
	
	/**
	 * 加载配置文件
	 */
	public static void loadConfigProperty() {
		try {
			CONFIG_PROPERTIES = new SafeProperties(PROJECT_CHARSET);
			String sPath = getProjectPath() + "/WEB-INF/classes/" + CONFIG_FILE_NAME;
			File file = new File(sPath);
			if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				CONFIG_PROPERTIES.load(fis);
				fis.close();
				fis = null;
			} else {
				System.out.println("配置文件不存在！");
			}
			file = null;
		} catch (Exception e) {
			System.out.println("读取配置文件出错！");
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取配置文件的值(int)
	 * @param sPropertyName
	 * @return
	 */
	public static int getProIntValue(String sPropertyName) {
		String sValue = getProStrValue(sPropertyName);
		Integer iValue = ChangeBean.stringToInteger(sValue);
		if (iValue == null) { return 0; }
		return iValue.intValue();
	}
	
	/**
	 * 获取配置文件的值(long)
	 * @param sPropertyName
	 * @return
	 */
	public static long getProLonValue(String sPropertyName) {
		String sValue = getProStrValue(sPropertyName);
		Long lValue = ChangeBean.stringToLong(sValue);
		if (lValue == null) { return 0; }
		return lValue.longValue();
	}
	
	/**
	 * 获取配置文件的值(int)
	 * @param sPropertyName
	 * @return
	 */
	public static int getIntStrValue(String sPropertyName) {
		String sValue = getProStrValue(sPropertyName);
		Integer iValue = ChangeBean.stringToInteger(sValue);
		if (iValue == null) { return 0; }
		return iValue.intValue();
	}
	
	/**
	 * 获取项目url
	 * @param request
	 * @return
	 */
	public static String getProjectUrl(HttpServletRequest request) {
		String sPath = "../";
		try {
			sPath = request.getScheme();
			sPath += "://" + request.getServerName() + ":" + request.getServerPort();
			sPath += request.getContextPath();
			return sPath;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sPath;
	}
	
	/**
	 * 验证权限
	 * @param sFunPopedom
	 * @param session
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	public static boolean checkCompetence(String sFunPopedom, HttpSession session) {
//		try {
//			CommonVo userInfoVo = new CommonVo();
//			if (session != null) {
//				userInfoVo = (CommonVo) session.getAttribute(SESSION_USER_LOGIN_INFO);
//				if (userInfoVo == null) {
//					userInfoVo = new CommonVo();
//				}
//				HashMap hm = userInfoVo.getHashMap();
//				if (hm != null && hm.containsKey(sFunPopedom)) { return true; }
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return false;
//	}
	
	/**
	 * 验证页面权限
	 * @param sMenuNum
	 * @param session
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	public static boolean checkPageCompetence(String sMenuNum, HttpSession session) {
//		boolean hasCompetence = false;
//		if (sMenuNum == null || sMenuNum.length() < 1) { return hasCompetence; }
//		try {
//			HashMap hm = null;
//			CommonVo userInfoVo = new CommonVo();
//			if (session != null) {
//				userInfoVo = (CommonVo) session.getAttribute(SESSION_USER_LOGIN_INFO);
//				if (userInfoVo != null) {
//					hm = userInfoVo.getHashMap();
//				}
//			}
//			if (hm == null || hm.size() < 1) { return hasCompetence; }
//			String[] sMenuArr = sMenuNum.split("-");
//			if (sMenuArr == null || sMenuArr.length < 1) { return hasCompetence; }
//			for (int i = 0; i < sMenuArr.length; i++) {
//				String sTempNum = sMenuArr[i];
//				if (sTempNum == null || sTempNum.length() < 1) { return hasCompetence; }
//				if (!hm.containsKey(sTempNum)) { return hasCompetence; }
//			}
//			hasCompetence = true;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return hasCompetence;
//	}
	
	/**
	 * 获取导航位置
	 * @param sMenuNum
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	public static String getNavLocation(String sMenuNum, HttpSession session) {
//		String sNavLocation = "";
//		if (sMenuNum == null || sMenuNum.length() < 1) { return sNavLocation; }
//		try {
//			HashMap hm = null;
//			CommonVo userInfoVo = new CommonVo();
//			if (session != null) {
//				userInfoVo = (CommonVo) session.getAttribute(SESSION_USER_LOGIN_INFO);
//				if (userInfoVo != null) {
//					hm = userInfoVo.getHashMap();
//				}
//			}
//			if (hm == null || hm.size() < 1) { return sNavLocation; }
//			String[] sMenuArr = sMenuNum.split("-");
//			if (sMenuArr == null || sMenuArr.length < 1) { return sNavLocation; }
//			for (int i = 0; i < sMenuArr.length; i++) {
//				String sTempNum = sMenuArr[i];
//				if (sTempNum == null || sTempNum.length() < 1) {
//					continue;
//				}
//				if (i != 0) {
//					sNavLocation += "&nbsp;&gt;&nbsp;";
//				}
//				sNavLocation += ChangeBean.clearNull((String) hm.get(sTempNum));
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return sNavLocation;
//	}
	
	
	public static void main(String[] args) {
		// System.out.print(GlobalSetting.getUploadImporexpDirectory());
	}
}